package dicky.fortunetelling.logic;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;

public class BodyClockUtil {

	public static void main(String[] args) {

	}

	public static List GetBodyClock(String birthday, String today,
			int beforeday, int afterday) throws Exception {
		List list = new ArrayList();
		HashMap map = null;
		Calendar cal = Calendar.getInstance();
		String date = "";

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		Date d1 = df.parse(birthday);
		Date d0 = df.parse(today);

		cal.setTime(d0);
		cal.add(Calendar.DATE, beforeday);
		Date d2 = cal.getTime();

		cal.setTime(d0);
		cal.add(Calendar.DATE, afterday);
		Date d3 = cal.getTime();

		int d = daysBetween(d3, d2);
		double m, m1, m2, m3, i_0, m_0, p_0;
		double i_1, i_2, i_3, m_1, m_2, m_3, p_1, p_2, p_3, t1, t2;
		// 智力周期值
		i_0 = 33;
		i_1 = i_0 * 0.25;
		i_2 = i_0 * 0.5;
		i_3 = i_0 * 0.75;
		// 情绪周期值
		m_0 = 28;
		m_1 = m_0 * 0.25;
		m_2 = m_0 * 0.5;
		m_3 = m_0 * 0.75;
		// 体力周期值
		p_0 = 23;
		p_1 = p_0 * 0.25;
		p_2 = p_0 * 0.5;
		p_3 = p_0 * 0.75;
		// 高潮、正常、低潮分界值
		t1 = 40;
		t2 = 60;

		for (int i = 0; i < d; i++) {
			cal.setTime(d2);
			cal.add(Calendar.DATE, i);
			date = df.format(cal.getTime());
			// 计算与出生日期差
			m = daysBetween(cal.getTime(), d1);
			// 计算各周期值取余
			m1 = m % i_0;
			m2 = m % m_0;
			m3 = m % p_0;
			// 换算各周期峰值
			// System.out.print(date+",m1="+m1);
			if (m1 <= i_1) {
				m1 = m1 + i_1;
			} else if (m1 <= i_3) {
				m1 = i_3 - m1;
			} else {
				m1 = m1 - i_3;
			}

			if (m2 <= m_1) {
				m2 = m2 + m_1;
			} else if (m2 <= m_3) {
				m2 = m_3 - m2;
			} else {
				m2 = m2 - m_3;
			}

			if (m3 <= p_1) {
				m3 = m3 + p_1;
			} else if (m3 <= p_3) {
				m3 = p_3 - m3;
			} else {
				m3 = m3 - p_3;
			}
			// 统一换算百分比周期值
			// System.out.print(",m1_1="+m1);
			m1 = m1 / i_2 * 100;
			m2 = m2 / m_2 * 100;
			m3 = m3 / p_2 * 100;
			// System.out.println(",m1_2="+m1);
			map = new HashMap();
			map.put("date", date);
			map.put("m", m);
			map.put("i_value", m1);
			if (m1 <= t1) {
				map.put("i_flag", "低潮");
			}
			if (m1 > t1 && m1 < t2) {
				map.put("i_flag", "正常");
			}
			if (m1 >= t2) {
				map.put("i_flag", "高潮");
			}

			map.put("m_value", m2);
			if (m2 <= t1) {
				map.put("m_flag", "低潮");
			}
			if (m2 > t1 && m2 < t2) {
				map.put("m_flag", "正常");
			}
			if (m2 >= t2) {
				map.put("m_flag", "高潮");
			}

			map.put("p_value", m3);
			if (m3 <= t1) {
				map.put("p_flag", "低潮");
			}
			if (m3 > t1 && m3 < t2) {
				map.put("p_flag", "正常");
			}
			if (m3 >= t2) {
				map.put("p_flag", "高潮");
			}
			list.add(map);
		}

		return list;
	}

	private static int daysBetween(Date now, Date returnDate) {
		Calendar cNow = Calendar.getInstance();
		Calendar cReturnDate = Calendar.getInstance();
		cNow.setTime(now);
		cReturnDate.setTime(returnDate);
		setTimeToMidnight(cNow);
		setTimeToMidnight(cReturnDate);
		long todayMs = cNow.getTimeInMillis();
		long returnMs = cReturnDate.getTimeInMillis();
		long intervalMs = todayMs - returnMs;
		return millisecondsToDays(intervalMs);
	}

	private static int millisecondsToDays(long intervalMs) {
		return (int) (intervalMs / (1000 * 86400));
	}

	private static void setTimeToMidnight(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
	}

	public static void SetBirtyday(Context ctx,String date) {
		SharedPreferences sp = ctx.getSharedPreferences("Birthday", 0);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString("Birthday", date);
		editor.commit();
	}

	public static String GetBirtyday(Context ctx) {
		String date = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		SharedPreferences sp = ctx.getSharedPreferences("Birthday", 0);
		date = sp.getString("Birthday", df.format(new Date()));
		return date;
	}
}
